//
//  WFGuideDetailVC.m
//  谷居
//
//  Created by dllo on 16/7/2.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFGuideDetailVC.h"
#import "MBProgressHUD.h"
#import "WFImageView.h"
#import "WFButtonImage.h"
#import "NetWorkingTool.h"
#import "WFUIView.h"
#import <WebKit/WebKit.h>

//#import "WKWebView.h"
@interface WFGuideDetailVC ()<WKNavigationDelegate>
@property (nonatomic, retain) UIView *baseView;

@property (nonatomic, retain) WFUIView *navigationView; /** 导航栏改变透明度的view. */

@property (nonatomic, retain) WFButtonImage *leftBackButton; /** navigation左侧返回按钮. */

@property (nonatomic, retain) WFButtonImage *rightShareButton; /** navigation右侧分享按钮. */

@property (nonatomic, retain) WKWebView *webView;
@end

@implementation WFGuideDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createRoationView];
#pragma mark - 创建titleView
    [self createNavigationView];
    // Do any additional setup after loading the view.
    [self createData];
}
/** 创建进入的界面. */
- (void)createRoationView {
    _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    [self.view addSubview:_baseView];
    [_baseView release];
    _baseView.backgroundColor = [UIColor whiteColor];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:_baseView animated:YES];
    hud.mode = MBProgressHUDAnimationFade;
    hud.labelText = @"刷新中";
    
    
}
/** 创建titleView. */
- (void)createNavigationView {
    WFImageView *leftNavigationBarButton = [[WFImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
    leftNavigationBarButton.image = [UIImage imageNamed:@"返回2.png"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftNavigationBarButton];;
    UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popToRoot:)];
    leftNavigationBarButton.userInteractionEnabled = YES;
    [leftNavigationBarButton addGestureRecognizer:tapLeft];
    
    
    WFImageView *rightNavigationBarButton = [[WFImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
    rightNavigationBarButton.image = [UIImage imageNamed:@"分享蓝.png"];
    UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sharedTo:)];
    rightNavigationBarButton.userInteractionEnabled = YES;
    [rightNavigationBarButton addGestureRecognizer:rightTap];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightNavigationBarButton];
    
    /** 创建底层uiview. */
    _navigationView = [[WFUIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [self.view addSubview:_navigationView];
    [_navigationView release];
    _navigationView.alpha = 0;
    /** 创建左侧按钮. */
    _leftBackButton = [WFButtonImage buttonWithType:UIButtonTypeCustom];
    _leftBackButton.frame = CGRectMake(16, 26, 32, 32);
    [_leftBackButton setBackgroundImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    /** button 添加点击方法. */
    [_leftBackButton addTarget:self action:@selector(leftBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    /** 创建右侧按钮. */
    _rightShareButton = [WFButtonImage buttonWithType:UIButtonTypeCustom];
    _rightShareButton.frame = CGRectMake(327, 26, 32, 32);
    [self.view addSubview:_rightShareButton];
    [self.view addSubview:_leftBackButton];
    [_rightShareButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_rightShareButton setBackgroundImage:[UIImage imageNamed:@"分享黑.png"] forState:UIControlStateNormal];
    [self.view bringSubviewToFront:_leftBackButton];
    [self.view bringSubviewToFront:_rightShareButton];
}

#pragma mark - 在视图将要出现的时候隐藏navigationBar
- (void)viewWillAppear:(BOOL)animated {
//    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated {
//    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)leftBarButtonAction:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)rightBarButtonAction:(UIButton *)button {
    NSLog(@"分享到微博按钮. ");
}

/** 点击pop. */
- (void)popToRoot:(UITapGestureRecognizer *)tap {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/** 分享按钮. */
- (void)sharedTo:(UITapGestureRecognizer *)tap {
    NSLog(@"点击分享按钮");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 创建数据
/** 创建网络请求. */
- (void)createData {
    [NetWorkingTool netWorkingWithURL:self.URL block:^(id result) {
        
        NSLog(@"%@", self.URL);
        NSLog(@"%@",result[@"strategyModel"][@"context"]);
//        NSString *js = @"var count = document.images.length;for (var i = 0; i < count; i++) {}"
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self.view addSubview:_webView];
        [_webView release];
        _webView.navigationDelegate = self;
        NSString *htmlStr = result[@"strategyModel"][@"context"];
        [_webView loadHTMLString:htmlStr baseURL:nil];
//        _webView.scalesPageToFit = YES;
#pragma mark - 移除baseView
        [MBProgressHUD hideHUDForView:_baseView animated:YES];
        [_baseView removeFromSuperview];
        
    }];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [ webView evaluateJavaScript:@"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = /function ResizeImages() { "
         "var myimg,oldwidth;"
         "var maxwidth = 1000.0;" // UIWebView中显示的图片宽度
         "for(i=1;i <document.images.length;i++){"
         "myimg = document.images[i];"
         "oldwidth = myimg.width;"
         "myimg.width = maxwidth;"
         "}"
         "}/"
         "document.getElementsByTagName('head')[0].appendChild(script);ResizeImages();" completionHandler:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
